package com.libinshen.wxpay.videoAliBaBa.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.libinshen.wxpay.common.vo.CommonResult;
import com.libinshen.wxpay.videoAliBaBa.entity.Teacher;
import com.libinshen.wxpay.videoAliBaBa.entity.vo.TeacherQueryVo;
import com.libinshen.wxpay.videoAliBaBa.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api("教师管理模块")
@RestController
@RequestMapping("/admin/teacher")
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    @ApiOperation("所有讲师列表")
    @GetMapping("list")
    public CommonResult listAll() {
        List<Teacher> list = teacherService.list();
        return CommonResult.ok().data("items", list);
    }

    @ApiOperation(value = "根据ID删除讲师", notes = "根据ID删除讲师，逻辑删除")
    @DeleteMapping("remove/{id}")
    public CommonResult removeById(@ApiParam(value = "讲师ID", required = true) @PathVariable String id) {
        boolean result = teacherService.removeById(id);
        if (result) {
            return CommonResult.ok().setMessage("删除成功");
        } else {
            return CommonResult.error().setMessage("数据不存在");
        }
    }

    @ApiOperation("讲师分页列表")
    @GetMapping("list/{page}/{limit}")
    public CommonResult listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                                 @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
                                 @ApiParam("讲师列表查询对象") TeacherQueryVo teacherQueryVo) {

        Page<Teacher> pageParam = new Page<>(page, limit);
        IPage<Teacher> pageModel = teacherService.selectPage(pageParam, teacherQueryVo);
        List<Teacher> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return CommonResult.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("新增讲师")
    @PostMapping("save")
    public CommonResult save(@ApiParam("讲师对象") @RequestBody Teacher teacher) {
        teacherService.save(teacher);
        return CommonResult.ok().setMessage("保存成功");
    }

    @ApiOperation("更新讲师")
    @PutMapping("update")
    public CommonResult updateById(@ApiParam("讲师对象") @RequestBody Teacher teacher) {
        boolean result = teacherService.updateById(teacher);
        if (result) {
            return CommonResult.ok().setMessage("修改成功");
        } else {
            return CommonResult.error().setMessage("数据不存在");
        }
    }

    @ApiOperation("根据id获取讲师信息")
    @GetMapping("get/{id}")
    public CommonResult getById(@ApiParam("讲师对象") @PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        if (teacher != null) {
            return CommonResult.ok().data("item", teacher);
        } else {
            return CommonResult.error().setMessage("数据不存在");
        }
    }

    @ApiOperation("easyPoi导入数据")
    @PostMapping("/importEasyPoi")
    public CommonResult importEasyPoi(@RequestBody MultipartFile file) {
        teacherService.importEasyPoi(file);
        return CommonResult.ok().setMessage("文件上传成功");
    }

    @ApiOperation("easyPoi导出数据")
    @PostMapping("/exportDataEasypoi")
    public CommonResult exportEasypoi(HttpServletResponse response) {
        teacherService.exportEasypoi(response);
        return CommonResult.ok();
    }

    @ApiOperation("easyExcel导入数据")
    @PostMapping("/importEasyExcel")
    public CommonResult importEasyExcel(@RequestParam("file") MultipartFile file) {
        try {
            teacherService.importEasyExcel(file);
            return CommonResult.ok().setMessage("文件上传成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return CommonResult.ok().setMessage("文件上传成功");
    }

    //todo 待优化以及做图片下载，字段格式化等
    @ApiOperation("easyExcel导出数据,  ")
    @PostMapping("/exportEasyExcel")
    public CommonResult exportEasyExcel(HttpServletResponse response) {
        teacherService.exportEasyExcel(response);
        return CommonResult.ok();
    }
}
